//
//  HeaderTableViewCell.m
//  FVQuickMessage
//
//  Created by dllo on 16/8/10.
//  Copyright © 2016年 zty. All rights reserved.
//

#import "HeaderTableViewCell.h"
#import "CreateViewController.h"

@interface HeaderTableViewCell ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, retain) UIButton *headerImageView;
@property (nonatomic, retain) UILabel *nameLabel;
@property (nonatomic, retain) UILabel *numberLabel;
// @property (nonatomic, retain) UIImageView *erImageView;
@property (nonatomic, retain) UIButton *erButton;

@end

@implementation HeaderTableViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.headerImageView = [[UIButton alloc] init];
        [self.contentView addSubview:_headerImageView];
        
        [self.headerImageView addTarget:self action:@selector(clickHeaderImageView) forControlEvents:UIControlEventTouchDown];
        
        
        self.nameLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_nameLabel];
        
        self.numberLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_numberLabel];
        
        /*self.erImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_erImageView];*/
        
        self.erButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_erButton];
        
        [self config];
    }
    return self;
}

-(void)config {
    
    [self.headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.contentView).with.offset(10);
        make.left.equalTo(self.contentView).with.offset(10);
        make.width.offset(50);
        make.height.offset(50);
        
    }];
    self.headerImageView.layer.masksToBounds = YES;
    self.headerImageView.layer.cornerRadius = 25;
    self.headerImageView.backgroundColor = [UIColor orangeColor];
    // self.headerImageView.image = [UIImage imageNamed:@"Icon"];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        
        make.top.equalTo(self.contentView).with.offset(20);
        make.left.equalTo(self.headerImageView.mas_right).with.offset(10);
        make.width.offset(WIDTH * 0.6);
        make.height.offset(20);
        
    }];
    // self.nameLabel.backgroundColor = [UIColor greenColor];
    
    NSString *username = [[EaseMob sharedInstance].chatManager loginInfo][@"username"];
    
    self.nameLabel.text = username;
    
    
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.nameLabel).with.offset(25);
        make.left.equalTo(self.headerImageView.mas_right).with.offset(10);
        make.width.offset(WIDTH * 0.6);
        make.height.offset(20);
        
    }];
    // self.numberLabel.backgroundColor = [UIColor greenColor];
    self.numberLabel.text = username;
    
    /*[self.erImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.contentView).with.offset(20);
        make.right.equalTo(self.contentView).with.offset(-10);
        make.width.offset(30);
        make.height.offset(30);
        
    }];
    
    self.erImageView.image = [UIImage imageNamed:@"二维码"];*/
    
    [self.erButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.contentView).with.offset(20);
        make.right.equalTo(self.contentView).with.offset(-10);
        make.width.offset(30);
        make.height.offset(30);
        
    }];
    
    [self.erButton setImage:[UIImage imageNamed:@"二维码"] forState:UIControlStateNormal];
    [self.erButton addTarget:self action:@selector(myErAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)myErAction:(UIButton *)button {
    
    CreateViewController *createVC = [[CreateViewController alloc] init];
    
    [[self naviController] pushViewController:createVC animated:YES];
    
}

- (UINavigationController*)naviController {
    
    for (UIView* next = [self superview]; next; next = next.superview) {
        
        UIResponder* nextResponder = [next nextResponder];
        
        if ([nextResponder isKindOfClass:[UINavigationController class]]) {
            
            return (UINavigationController*)nextResponder;
        }
    }
    
    return nil;
}

- (void) clickHeaderImageView {
    
    NSLog(@"click");
    
    UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
    pickerController.allowsEditing = YES;
    pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    pickerController.delegate = self;
    
    [[self naviController] presentViewController:pickerController animated:YES completion:^{
        
        
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    NSLog(@"%@", info);
    
    
    
    UIImage *resultImage = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    
    [self.headerImageView setImage:[resultImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
    
    // [button setImage:resultImage forState:UIControlStateNormal];
    
    [[self naviController] dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}

- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
    [[self naviController] dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

@end
